
#define TIME_TEST 1
#include "sd3078.h"
#include "string.h"
#include "stdio.h"

/*cmd*/
#define  RTC_SD3078_RD_TIME_CMD                     "RTC_RD_TIME"   /*read time*/
#define  RTC_SD3078_SET_TIME_CMD                    "RTC_SET_TIME"   /*set time*/
#define  RTC_SD3078_RD_TEMP_CMD                     "RTC_SD3078_RD_TEMP"   /*read chip temperature*/
#define  RTC_SD3078_BAT_I2C_CMD                     "RTC_SD3078_BAT_I2C"     /*battery  i2c ctrl*/
#define  RTC_SD3078_BAT_VOL_CMD                     "RTC_SD3078_BAT_VOL"   /*battery voltage read*/
#define  RTC_SD3078_EN_CHARGE_CMD                   "RTC_SD3078_EN_CHARGE"      /*battery charge control*/
#define RTC_SD3078_TEMP_AL_CMD                      "RTC_SD3078_TEMP_AL"  /*set lowest temperature  alarm  value*/
#define RTC_SD3078_TEMP_AH_CMD                      "RTC_SD3078_TEMP_AH"    /*set highest temperature  alarm value*/
#define RTC_SD3078_TEMP_HIS_L_CMD                   "RTC_SD3078_TEMP_HIS_L"   /*history lowest temperature  value*/
#define RTC_SD3078_TEMP_HIS_H_CMD           	    "RTC_SD3078_TEMP_HIS_H" /*history highest temperature  value*/
#define RTC_SD3078_TEMP_HIS_L_T_CMD                 "RTC_SD3078_TEMP_HIS_L_T"    /*the occurrence time of the lowest temperature in history*/
#define RTC_SD3078_TEMP_HIS_H_T_CMD                 "RTC_SD3078_TEMP_HIS_H_T"  /*the occurrence time of the highest temperature in history*/
#define RTC_SD3078_RAM_RD_CMD                       "RTC_SD3078_RAM_RD"   /*user ram read*/
#define RTC_SD3078_RAM_WR_CMD                       "RTC_SD3078_RAM_WR"   /*user ram write*/
#define RTC_SD3078_DEVICE_ID_CMD                    "RTC_SD3078_DEVICE_ID"  /*read device id*/
#define RTC_SD3078_ALARM_EXT_SET_CMD                "RTC_SD3078_ALARM_EXT_SET"   /*alarm extend set*/
#define RTC_SD3078_CLK_OUT_CMD                      "RTC_SD3078_CLK_OUT"    /*freq output ctrl*/
#define RTC_SD3078_COUNT_DOWN_SET_CMD               "RTC_SD3078_COUNT_DOWN_SET"  /*count down set*/

static void dci_rtc_read_time(void)
{
    sd3078_time_t time_buf;
    sd3078_exit_code_e ret;
    static sd3078_time_t time_comp;
    
    ret = sd3078_get_time(&time_buf);
    if (ret != SD3078_EXIT_OK)
    {
         printf("sd3078 read time err!\n");
    }
       
    if(time_buf.second != time_comp.second)
    {
        printf("time:%d-%d-%d-%d  %d:%d:%d,am_pm:%d,12_24:%d\n",time_buf.year, time_buf.month, time_buf.day,time_buf.week,
        time_buf.hour,time_buf.minute,time_buf.second, time_buf.am_pm, time_buf.hour_type);
        time_comp.second = time_buf.second;
        
    }
}
static void dci_rtc_set_time(void)
{
    sd3078_time_t time_buf;
    sd3078_exit_code_e ret;

    time_buf.second = 52;
    time_buf.minute = 59;
    time_buf.hour = 12;
    time_buf.week = 0;
    time_buf.day = 12;
    time_buf.month = 2;
    time_buf.year = 23;
    time_buf.hour_type = SD3078_HOUR_12;  
    time_buf.am_pm = SD3078_PM,
    ret = sd3078_set_time(&time_buf);
    if (ret != SD3078_EXIT_OK)
    {
         printf("sd3078 set time err!\n");
    } 
}


static int ioctrl_handler(const char *str)
{
    sd3078_exit_code_e ret = 0;
    printf("cmd=%s\n", str);
    if(!strncmp(str, RTC_SD3078_RD_TIME_CMD , strlen(str)))
    {
        dci_rtc_read_time();
    }
    else if(!strncmp(str, RTC_SD3078_SET_TIME_CMD , strlen(str)))
    {
        dci_rtc_set_time();
    }
     else if(!strncmp(str, RTC_SD3078_RD_TEMP_CMD , strlen(str)))
    {
        signed char temp = 0;
        
        ret = sd3078_get_temperature(&temp);
        if(ret == SD3078_EXIT_OK)
        {
            printf("temperature =%d\n",temp);
        }
    }
    else if(!strncmp(str, RTC_SD3078_BAT_I2C_CMD, strlen(str)))
    {
        unsigned char i2c_en = 1;
        
        sd3078_battery_i2c_ctrl(i2c_en);
        if(ret == SD3078_EXIT_OK)
        {
            printf("battery i2c mode is set =%d\n", i2c_en);
        }
    }
    else if(!strncmp(str, RTC_SD3078_BAT_VOL_CMD, strlen(str)))
    {
        unsigned short  vol;
    
        sd3078_read_battery_voltage(&vol);
        if(ret == SD3078_EXIT_OK)
        {
            printf("battery voltage*100 =%dv\n", vol);
        }
    }
    else if(!strncmp(str, RTC_SD3078_EN_CHARGE_CMD, strlen(str)))
    {
        sd3078_charge_t charge;

        charge.chage_en = 1;
        charge.resistance = 0x02;
        sd3078_battery_charge_ctrl(&charge);
        if(ret == SD3078_EXIT_OK)
        {
            printf("battery charge ,enable=%d,resistance=%d\n", charge.chage_en, charge.resistance);
        }
    }
    else  if(!strncmp(str, RTC_SD3078_TEMP_AL_CMD, strlen(str)))
    {
        sd3078_temp_alarm_t temp_al;

        temp_al.type = 0;
        temp_al.oe = 1;
        temp_al.ie = 1;
        temp_al.temp = -40;
        ret = sd3078_set_alarm_temp( &temp_al,  SD3078_TEMP_AL);
        if(ret == SD3078_EXIT_OK)
        {
            printf("set temperature low alarm=%d,enable=%d,int enable=%d\n", temp_al.temp, temp_al.oe, temp_al.ie);
        }
    }
    else  if(!strncmp(str, RTC_SD3078_TEMP_AH_CMD, strlen(str)))
    {
       sd3078_temp_alarm_t temp_ah;

        temp_ah.type =1;
        temp_ah.oe = 1;
        temp_ah.ie = 1;
        temp_ah.temp = 85;
        ret = sd3078_set_alarm_temp( &temp_ah,  SD3078_TEMP_AH);
        if(ret == SD3078_EXIT_OK)
        {
            printf("set temperature high alarm=%d,enable=%d, int enable=%d\n", temp_ah.temp, temp_ah.oe, temp_ah.ie);
        }
    }
    else if(!strncmp(str, RTC_SD3078_TEMP_HIS_L_CMD, strlen(str)))
    {
        signed char temp_his_l= 0;

        ret = sd3078_get_his_temperature(&temp_his_l, SD3078_TEMP_HIS_L);
        if(ret == SD3078_EXIT_OK)
         {
            printf("read history lowest temperature =%d\n", temp_his_l);
         }
    }
    else  if(!strncmp(str, RTC_SD3078_TEMP_HIS_H_CMD, strlen(str)))
    {
        signed char temp_his_h= 0;

        ret = sd3078_get_his_temperature(&temp_his_h, SD3078_TEMP_HIS_H);
        if(ret == SD3078_EXIT_OK)
         {
            printf("read history highest temperature =%d\n", temp_his_h);
         }
    }
    else if(!strncmp(str, RTC_SD3078_TEMP_HIS_L_T_CMD, strlen(str)))
    {
        sd3078_time_t time_l;

        memset(&time_l, 0, sizeof(time_l));
        ret = sd3078_get_his_temp_time(&time_l, SD3078_TEMP_HIS_L);
        if(ret == SD3078_EXIT_OK)
         {
            printf("read history lowest temperature occor time:\n ,%d-%d-%d-%d,%d:%d:%d " , \
            time_l.year, time_l.month, time_l.week,  time_l.day, \
            time_l.hour, time_l.minute, time_l.second);   
         }
    }
    else  if(!strncmp(str, RTC_SD3078_TEMP_HIS_H_T_CMD, strlen(str)))
    {
        sd3078_time_t time_h;

        memset(&time_h, 0, sizeof(time_h));
        ret = sd3078_get_his_temp_time(&time_h, SD3078_TEMP_HIS_H);
        if(ret == SD3078_EXIT_OK)
         {
            printf("read history highest temperature occor time:\n ,%d-%d-%d-%d,%d:%d:%d " , \
            time_h.year, time_h.month, time_h.week, time_h.day, \
            time_h.hour, time_h.minute, time_h.second);   
         }
    }
    else if(!strncmp(str, RTC_SD3078_RAM_RD_CMD, strlen(str)))
    {
        unsigned char buf[70];
        unsigned char i;
        sd3078_ram_t ram;

        memset(&buf, 0, sizeof(buf));
        ram.st_addr = 0x2c;
        ram.end_addr = 0x71;
        ram.buf = buf;

        ret = sd3078_read_ram(&ram);
        if (ret == SD3078_EXIT_OK)
         {
            printf("read user ram data:");
          for(i=0; i<70; i++) 
          {
            if (i %10 == 0)
            {
                printf("\n");
            }
            printf("%x ",buf[i]);
          
          }
          printf("\n");
         }
    }
    else  if(!strncmp(str, RTC_SD3078_RAM_WR_CMD, strlen(str)))
    {
        unsigned char buf[70];
        unsigned char i;
        sd3078_ram_t ram;

        ram.st_addr = 0x2c;
        ram.end_addr = 0x71;
        ram.buf = buf;
        for(i = 0; i < 70; i++) 
        {
            buf[i] = i;  
        }
        ret = sd3078_write_ram(&ram);
        if (ret == SD3078_EXIT_OK)
        {
            printf("write user ram data:");
            for(i=0; i<70; i++) 
            {
                if (i %10 == 0)
                {
                    printf("\n");
                }
                printf("%x ",buf[i]);
            }
            printf("\n");
        }
    }
    else  if(!strncmp(str, RTC_SD3078_DEVICE_ID_CMD, strlen(str)))
    {
        unsigned char id[8];

        ret = sd3078_read_device_id(id);
        if(ret == SD3078_EXIT_OK)
        {
            printf ("deviced id=%x-%x-%x-%x-%x-%x-%x-%x\n",id[0],id[1],id[2], id[3],id[4],id[5],id[6], id[7] );
        }
    }
    else  if(!strncmp(str, RTC_SD3078_ALARM_EXT_SET_CMD, strlen(str)))
    {
        sd3078_time_alarm_t alarm_time;

        sd3078_read_alarm_time(&alarm_time);
        if(ret == SD3078_EXIT_OK)
        {
            printf("read time: %d-%d-%d-%d,%d:%d:%d \n" , \
            alarm_time.year_a, alarm_time.mon_a, alarm_time.week_a, alarm_time.day_a, \
            alarm_time.hour_a, alarm_time.min_a, alarm_time.sec_a);   
        }
  
    }
    else if(!strncmp(str, RTC_SD3078_CLK_OUT_CMD, strlen(str)))
    {
        sd3078_clk_out_t clk;

        clk.freq = SD3078_CLK_OUT_8;
        clk.oe = 1;
        ret = sd3078_clk_out(&clk);
        if(ret == SD3078_EXIT_OK)
        {
            printf ("set clk out=%d,enable=%d\n",clk.freq, clk.oe);
        }
    }
    else if(!strncmp(str, RTC_SD3078_COUNT_DOWN_SET_CMD, strlen(str)))
    {
        sd3078_count_down_t count_down;

        count_down.count = 4096*2;
        count_down.source = SD3078_COUNT_DOWN_4096;
        count_down.ie = 1;
        count_down.int_period = 1;

        ret = sd3078_count_down(&count_down);
        if(ret == SD3078_EXIT_OK)
        {
            printf ("set count down, cnt=%dsource=%d,ie=%d,period=%d\n",count_down.count,count_down.source, count_down.ie, count_down.int_period);
        }
    }
    else
    {
        printf("error cmd!=%s\n",str);
    }
    return ret;
}




void rtc_test(void)
{
    ioctrl_handler(RTC_SD3078_SET_TIME_CMD);
    ioctrl_handler(RTC_SD3078_RD_TIME_CMD);
    ioctrl_handler(RTC_SD3078_RD_TEMP_CMD);

    ioctrl_handler(RTC_SD3078_BAT_I2C_CMD);
    ioctrl_handler(RTC_SD3078_BAT_VOL_CMD);
    ioctrl_handler(RTC_SD3078_EN_CHARGE_CMD);

    ioctrl_handler(RTC_SD3078_TEMP_AL_CMD);
    ioctrl_handler(RTC_SD3078_TEMP_AH_CMD);
    ioctrl_handler(RTC_SD3078_TEMP_HIS_L_CMD);
    ioctrl_handler(RTC_SD3078_TEMP_HIS_H_CMD);
    
    ioctrl_handler(RTC_SD3078_TEMP_HIS_L_T_CMD);
    ioctrl_handler(RTC_SD3078_TEMP_HIS_H_T_CMD);

    ioctrl_handler(RTC_SD3078_RAM_RD_CMD);
    ioctrl_handler(RTC_SD3078_RAM_WR_CMD);
    ioctrl_handler(RTC_SD3078_DEVICE_ID_CMD);
    ioctrl_handler(RTC_SD3078_COUNT_DOWN_SET_CMD);
}